<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 首行按钮 -->
            <el-row style="height:60px" type="flex" align="middle">
              <el-button icon="el-icon-plus" type="primary" size="small" @click="addR">新增角色</el-button>
            </el-row>

            <!-- 数据表格 -->
            <el-table :data="tableDate" style="width: 100%">
              <el-table-column type="index" label="序号" width="120">
                <template v-slot="{$index}">
                  {{ ($index+1) + ((p.page -1) * 2) }}
                </template>
              </el-table-column>
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column prop="" label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignP(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editR(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteR(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                layout="prev, pager, next"
                :total="total"
                :page-size="p.pagesize"
                :current-page.sync="p.page"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 添加数据弹窗 -->
      <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" @close="closeDialog">
        <el-form ref="formRef" label-width="100px" :model="FormDate" :rules="FormRules">

          <el-form-item label="角色名称:" prop="name">
            <el-input v-model="FormDate.name" />
          </el-form-item>

          <el-form-item label="角色描述:" prop="description">
            <el-input v-model="FormDate.description" />
          </el-form-item>

        </el-form>
        <!-- 使用具名插槽 -->
        <template #footer class="dialog-footer">
          <div style=" text-align: center;">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
        </template>
      </el-dialog>

      <!-- 分配权限弹窗 -->
      <el-dialog title="分配权限" :visible.sync="dialogVisibleforAssignPer">
        <AssignPermission ref="APRef" :cur-id="curId" @successAddPerMission="successAddPerMission" />
      </el-dialog>

    </div>
  </div>
</template>

<script>
import AssignPermission from './AssignPermission'
import { queryRoleList, deleteRole, addRole, updateRole } from '@/api/settings'
export default {
  name: '',
  components: {
    AssignPermission
  },
  data() {
    return {
      total: 0, // 总条数
      tableDate: [], // 表格数据,
      // 请求数据的格式
      p: {
        page: 1,
        pagesize: 7
      },
      // 控制弹窗是否存在
      dialogVisible: false,
      // 表单数据对象
      FormDate: {
        name: '', // 角色名称
        description: ''// 角色描述
      },
      // 表单验证
      FormRules: {
        name: [
          { required: true, message: '输入信息不能为空!', trigger: 'blur' }
        ], // 角色名称
        description: [
          { required: true, message: '输入信息不能为空!', trigger: 'blur' }
        ]// 角色描述
      },
      isEdit: false,
      title: '新增角色',
      // 分配权限可控制器
      dialogVisibleforAssignPer: false,
      // 当前被选中分配权限的角色id
      curId: ''
    }
  },
  created() {
    this.getRoleListDate()
  },
  methods: {

    // ? 分配权限成功
    successAddPerMission() {
      this.dialogVisibleforAssignPer = false
    },

    // ? 分配权限
    assignP(id) {
      // console.log(id)
      this.curId = id
      this.dialogVisibleforAssignPer = true

      this.$nextTick(() => {
        this.$refs.APRef.getRoleInfoById()
      })
    },

    //* 1.根据页面信息请求角色信息数据
    async getRoleListDate() {
      try {
        const res = await queryRoleList(this.p)
        // console.log(res)
        this.tableDate = res.data.rows
        this.total = res.data.total
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    //* 2.deleteRole(id) 删除当前角色
    deleteR(id) {
      // console.log(id)
      this.$confirm('您要删除这个角色吗?', '提示', {
        type: 'warning',
        confirmButtonText: '删除',
        cancelButtonText: '取消'
      }).then(
        async() => {
          // 调用接口删除
          // console.log(this.total)
          // debugger
          // 当这页只剩一条数据的时候
          if (this.total % this.p.pagesize === 1) {
            // 如果是第一页的最后一条数据 页数就不减了
            this.p.page > 1 ? this.p.page -= 1 : 1
          }

          const res = await deleteRole(id)
          this.$message.success(res.message)
          // 更新列表
          this.getRoleListDate()
        },
        () => {
          this.$message.info('您取消了操作')
        }
      )
    },
    //* 3点击新增角色按钮
    addR() {
      this.isEdit = false
      this.title = '新增角色'
      this.dialogVisible = true
    },
    //* 4.点击编辑角色按钮
    editR(data) {
      // 数据回填
      this.isEdit = true
      this.title = '编辑角色'
      this.dialogVisible = true
      this.FormDate = { ...data }
    },

    // todo 提交表单
    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          try {
            // 发送表单数据验证添加
            let res
            if (this.isEdit) {
              res = await updateRole(this.FormDate)
            } else {
              // 更新列表
              this.total++
              this.p.page = Math.ceil((this.total) / this.p.pagesize)
              res = await addRole(this.FormDate)
            }
            this.$message.success(res.message)
            this.dialogVisible = false // 关闭弹窗
            this.getRoleListDate() // 更新
          } catch (error) {
            this.$message.error(error.message)
          }
        } else {
          this.$message.error('请完善输入信息.')
        }
      })
    },

    //! 1. 点击页码时
    handleCurrentChange(val) {
      // 更新请求数据 信息
      this.p.page = val
      this.getRoleListDate()
      // console.log(val)
    },
    //! 2.点击上一页
    handlePrevClick(val) {
      this.p.page = val
      this.getRoleListDate()
      // console.log(val)
    },
    //! 3.点击下一页
    handleNextClick(val) {
      this.p.page = val
      this.getRoleListDate()
      // console.log(val)
    },
    //! 4.关闭弹窗的回调
    closeDialog() {
      // console.log('11111')
      // 重置表单
      this.$refs.formRef.resetFields()
    }
  }

}
</script>

<style  scoped>

</style>
